package com.cjjd.c.feature;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.cjjd.c.R;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.utils.DialogUtil;

import butterknife.Bind;

/**
 * ----------------------------------
 * Created by xyd on 2017/10/12.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class WebviewActivity extends ActionBarActivity {
    WebView webview;


    public static void Lauch(Context context,String url,String title){
        Intent intent=new Intent();
        intent.putExtra("weburl",url);
        intent.putExtra("title",title);
        intent.setClass(context,WebviewActivity.class);
        context.startActivity(intent);
    }

    private String url = "";
    private String title = "";

    @Override
    public Object getTitleString() {
        return " ";
    }

    @Override
    public int getContentView() {
        return R.layout.activity_webview;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webview=(WebView)findViewById(R.id.webview);
        setweb();
    }

    private void setweb() {
        url = getIntent().getStringExtra("weburl");
        title = getIntent().getStringExtra("title");

        if(TextUtils.isEmpty(title)){
            title=" ";
        }
        mActionBar.setTitle(title);

        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true); // 支持JS操作
        webSettings.setTextSize(WebSettings.TextSize.NORMAL);
        // 设置加载进来的页面自适应手机屏幕
        //webSettings.setUseWideViewPort(true);
        // 设置支持缩放
        webSettings.setSupportZoom(true);

        // 让缩放显示的最小值为起始
        //webview.setInitialScale((int) PhoneUtil.getDisplay() * 100);


            webview.loadUrl(url);
        webview.setVerticalScrollBarEnabled(false); // 设置竖直滚动条隐藏
        webview.setClickable(true);

        webview.canGoBack();

        webview.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {  //表示按返回键时的操作
                        webview.goBack();   //后退
                        return true;    //已处理
                    }
                }
                return false;
            }
        });

        webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(final WebView view, String url, Bitmap favicon) {
                //LogUtil.e("xyd", "onPageStarted...........");
                DialogUtil.showLoadingDialog(mContext, "加载中...");
                super.onPageStarted(view, url, favicon);
            }

            /**
             * onPageFinished指页面加载完成,完成后取消计时器
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                DialogUtil.dismissLoadingDialog(mContext);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                } else {
                    view.loadUrl(url);
                }
                return true;
            }
        });

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
    }

    private void getintentData() {



    }
}
